/**
 * 
 */
package lt.vin.exaatto.server.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import lt.vin.exaatto.server.dao.ChatDao;
import lt.vin.exaatto.shared.Chat;

/**
 * @author Vincentas
 *
 */
public class MockChatDaoImpl implements ChatDao {

	private Map<String, Set<Chat>> chats = new HashMap<String, Set<Chat>>();
	
	/* (non-Javadoc)
	 * @see lt.vin.exaatto.server.dao.ChatDao#save(lt.vin.exaatto.shared.Chat)
	 */
	@Override
	public void save(Chat chat) {
		getOwnerChats(chat.getOwner()).add(chat);
	}

	/* (non-Javadoc)
	 * @see lt.vin.exaatto.server.dao.ChatDao#get(java.lang.String)
	 */
	@Override
	public Chat get(String chatId) {
		for (Set<Chat> set : chats.values()) {
			for (Chat chat : set) {
				if (chat.getId() == chatId) {
					return chat;
				}
			}
		}
		
		return null;
	}
	
	private Set<Chat> getOwnerChats(String owner) {
		Set<Chat> set = chats.get(owner.toLowerCase());
		if (set == null) {
			set = new HashSet<Chat>();
			chats.put(owner.toLowerCase(), set);
		}
		
		return set;
	}

	/* (non-Javadoc)
	 * @see lt.vin.exaatto.server.dao.ChatDao#getChats(java.lang.String)
	 */
	@Override
	public List<Chat> getChats(String user) {
		return new ArrayList<Chat>(getOwnerChats(user));
	}

	/* (non-Javadoc)
	 * @see lt.vin.exaatto.server.dao.ChatDao#delete(lt.vin.exaatto.shared.Chat)
	 */
	@Override
	public void delete(String chatId) {
		getOwnerChats(get(chatId).getOwner()).remove(get(chatId));
	}

}
